#include "demo.h"
#include "interface_loader.h"
/*--------------------------------------------------------------------------------*/
IMPLEMENT_STATE_CHART( DEMO )
{
	IMPLEMENT_STATE( MENU_STATE )
	{
		IMPLEMENT_STATE_OnEvent( ENTER_EVENT )
		{
			if( Font.IsNull() )
			{
				INTERFACE_TEXT_BUTTON::REF
					button;

				GRAPHIC_FONT_MANAGER_RegisterFont( "font_arial2", "arial" );

				TestContainer = INTERFACE_LOADER::LoadContainerFromFile( "test.ui" );

				Font = GRAPHIC_FONT_MANAGER_GetFont( "arial" );
				TitleText.SetFont( *Font );
				TextContainer.SetFont( *Font );
				
				FpsText.SetFont( *Font );
				FpsText.SetPosition( MATH_VECTOR_2( 200.0f, 280.0f ) );
				FpsText.SetExtentPerChar( MATH_VECTOR_2( 13.0f, 30.0f ) );
				FpsText.SetColor( COLOR::ColorGreen );


				InfoText.SetFont( *Font );
				InfoText.SetPosition( MATH_VECTOR_2( 0.0f, -200.0f ) );
				InfoText.SetExtent( MATH_VECTOR_2( 256.0f, 15.0f ) );
				InfoText.SetColor( COLOR::ColorWhite );
				InfoText.SetText( "Press enter to start a new game." );
				InfoText.Update( 0 );

				button.New();

				button->Initialize(
					"New game",
					MATH_VECTOR_2( 0.0f, 0.0f ),
					MATH_VECTOR_2( 256.0f, 40.0f ),
					*Font,
					GRAPHIC_ANIMATION_MANAGER_GetAnimation( "button" ),
					GRAPHIC_ANIMATION_MANAGER_GetAnimation( "button_selected" ),
					MATH_VECTOR_2( 13.0f, 30.0f )
					);

				MenuContainer.Add( button );

				button.New();

				button->Initialize(
					"Quit",
					MATH_VECTOR_2( 0.0f, -64.0f ),
					MATH_VECTOR_2( 256.0f, 40.0f ),
					*Font,
					GRAPHIC_ANIMATION_MANAGER_GetAnimation( "button" ),
					GRAPHIC_ANIMATION_MANAGER_GetAnimation( "button_selected" ),
					MATH_VECTOR_2( 13.0f, 30.0f )
					);

				MenuContainer.Add( button );

				MenuContainer.SelectItem( 0 );
				MenuContainer.BindEvent( INTERFACE_MENU::EVENT_OnItemActivated, this, BUTTON_EVENT );
				
				
				TextContainer.AddText( "Hello", MATH_VECTOR_2( 200, 200 ), MATH_VECTOR_2( 13.0f, 30.0f ) );
				TextContainer.AddText( "World", MATH_VECTOR_2( 200, 170 ), MATH_VECTOR_2( 13.0f, 30.0f ) );
				TextContainer.Update( 0 );

				AtlasTest.SetAtlas( GRAPHIC_ATLAS::CreateTextureSetFromFile( "font1", 16, 16 ) );
				AtlasTest.AddQuad( MATH_VECTOR_2( 200, 20 ), MATH_VECTOR_2( 64, 64 ) , 66 );
				AtlasTest.AddQuad( MATH_VECTOR_2( 200, -80 ), MATH_VECTOR_2( 64, 64 ) , 17 );
			}

			TitleText.SetExtent( MATH_VECTOR_2( 300.0f, 25.0f ) );
			TitleText.SetPosition( MATH_VECTOR_2( -285.0f, 190.0f ) );
			TitleText.SetText( "ggpvision demo application" );
			TitleText.SetColor( COLOR::ColorRed );
			TitleText.SetAngle( MATH_Pi / 4.0f );
			TitleText.Update( 0 );



		}

		IMPLEMENT_STATE_OnEvent( UPDATE_EVENT )
		{
			INPUT_SYSTEM::GetInstance().Update( CurrentTimer->GetSecondCount() );

			if( INPUT_SYSTEM::GetInstance().IsKeyJustReleased( INPUT_KEY_Escape ) )
			{
				APPLICATION_Quit();
			}

			if( INPUT_SYSTEM::GetInstance().IsKeyJustPressed( INPUT_KEY_Space ) )
			{
				DEBUG_LogLine( "Mouse : " + TEXT::GetTextFromReal( INPUT_SYSTEM::GetInstance().GetCursorPosition().X ) );
			}

			MenuContainer.Update( *CurrentTimer );
			TestContainer->Update( *CurrentTimer );
		}

		IMPLEMENT_STATE_OnEvent( RENDER_EVENT )
		{
			TitleText.Render();
			InfoText.Render();
			TextContainer.Render();

			OPENGL_Color( COLOR::ColorWhite );

			MenuContainer.Render();
			TestContainer->Render();
			AtlasTest.Render();
		}

		IMPLEMENT_STATE_OnEvent( BUTTON_EVENT )
		{
			DEBUG_Log( "ButtonEvent : " );
			DEBUG_LogLine( EventObject.GetObject< INTERFACE_TEXT_BUTTON >().GetText() );
			
			switch ( MenuContainer.GetSelectedItemIndex() )
			{
				case MENU_NEW_GAME:
				{
					ChangeState( GAME_STATE );
				}
				break;
				
				case MENU_QUIT:
				{
					APPLICATION_Quit();
				}
			}
		}

		IMPLEMENT_STATE_OnEvent( EXIT_EVENT )
		{

		}
	}

	IMPLEMENT_STATE( GAME_STATE )
	{
		INDEX
			bullet_index;

		IMPLEMENT_STATE_OnEvent( ENTER_EVENT )
		{
			TitleText.SetExtent( MATH_VECTOR_2( 256.0f, 39.0f ) );
			TitleText.SetPosition( MATH_VECTOR_2( 0.0f, 270.0f ) );
			TitleText.SetText( "Game!" );
			TitleText.SetColor( COLOR::ColorRed );
			TitleText.SetAngle( 0.0f );
			TitleText.Update( 0 );

			Man.PlayAnimation( * GRAPHIC_ANIMATION_MANAGER_GetAnimation( "man" ) );
			Man.SetExtent( MATH_VECTOR_2( 32.0f, 32.0f ) );
			Man.SetPosition( MATH_VECTOR_2( 0.0f, 32.0f ) );
			Man.SetVelocity( 0.0f );
			Man.SetAngle( 0.0f );


		}

		IMPLEMENT_STATE_OnEvent( UPDATE_EVENT )
		{
			INPUT_SYSTEM::GetInstance().Update( STATE_GetTimeStep() );
		
			if( INPUT_SYSTEM::GetInstance().IsKeyPressed( INPUT_KEY_Return ) )
			{
				COMPONENT_ENTITY_2D::REF
					bullet;

				bullet.New();

				bullet->PlayAnimation( * GRAPHIC_ANIMATION_MANAGER_GetAnimation( "man" ) );
				bullet->SetExtent( MATH_VECTOR_2( 10.0f, 10.0f ) );
				bullet->SetPosition( Man.GetPosition() );
				bullet->SetVelocity( 1000.0f );
				bullet->SetAngle( Man.GetAngle() );
				bullet->SetDirection( Man.GetDirection() );
				bullet->SetColor( COLOR( 0.5f + 0.5f * MATH_GetRandom(), 0.5f + 0.5f * MATH_GetRandom(), 0.5f + 0.5f * MATH_GetRandom(), 1.0f ) );

				BulletTable.Add( bullet );
			}

			for_each_table_index( bullet_index, BulletTable )
			{
				BulletTable[ bullet_index ]->Update( STATE_GetTimeStep() );
				
				if( BulletTable[ bullet_index ]->GetPosition().Y > 300.0f )
				{
					BulletTable[ bullet_index ]->SetPosition( MATH_VECTOR_2( BulletTable[ bullet_index ]->GetPosition().X, -300.0f ) );
				}
				
				if( BulletTable[ bullet_index ]->GetPosition().X > 400.0f )
				{
					BulletTable[ bullet_index ]->SetPosition( MATH_VECTOR_2( -400.0f, BulletTable[ bullet_index ]->GetPosition().Y ) );
				}
				
				if( BulletTable[ bullet_index ]->GetPosition().Y < -300.0f )
				{
					BulletTable[ bullet_index ]->SetPosition( MATH_VECTOR_2( BulletTable[ bullet_index ]->GetPosition().X, 300.0f ) );
				}
				
				if( BulletTable[ bullet_index ]->GetPosition().X < -400.0f )
				{
					BulletTable[ bullet_index ]->SetPosition( MATH_VECTOR_2( 400.0f, BulletTable[ bullet_index ]->GetPosition().Y ) );
				}
			}			

			if( INPUT_SYSTEM::GetInstance().IsKeyJustReleased( INPUT_KEY_Escape ) )
			{
				ChangeState( MENU_STATE );
			}

			Man.Update( STATE_GetTimeStep() );
			FpsText.Update( STATE_GetTimeStep() );
			TitleText.SetText( TEXT::GetTextFromReal( ( REAL ) BulletTable.GetItemCount() ) );
			TitleText.Update( STATE_GetTimeStep() );
		}

		IMPLEMENT_STATE_OnEvent( RENDER_EVENT )
		{
			TitleText.Render();

			FpsText.Render();

			for_each_table_index( bullet_index, BulletTable )
			{
				BulletTable[ bullet_index ]->Render();
			}

			Man.Render();
		}

		IMPLEMENT_STATE_OnEvent( EXIT_EVENT )
		{
			BulletTable.Clear();
		}
	}
}
/*--------------------------------------------------------------------------------*/

